package com.example.mybaiduweishi;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.example.mybaiduweishi.activity.BBXActivity;
import com.example.mybaiduweishi.com.Constants;
import com.example.mybaiduweishi.db.AddressDao;
import com.example.mybaiduweishi.db.BaiBaoXiangDao;
import com.example.mybaiduweishi.db.DaoMaster;
import com.example.mybaiduweishi.db.DaoSession;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.vo.BaiBaoXiang;
import com.example.mybaiduweishi.vo.Contacts;
import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import org.greenrobot.greendao.database.Database;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/5/10 0010.
 */

public class MyApplication extends Application{
    static DaoSession daoSession;
    static DaoSession mobiledaoSession;
    static Context context;

    //入口方法
    public void onCreate(){
        super.onCreate();
        context=getApplicationContext();
        initClient();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        initDatabase();
        initDatabase1();
        new Thread(new Runnable() {
            @Override
            public void run() {
                initDatabaseTable();
                //将外部数据库拷贝到内存中
                try {
                    MyUtil.copyFileToMobile(context,"mobile.db","mobile.db");
                   List<Contacts> list=MyUtil.getAllContacts();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void initClient(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L,TimeUnit.MILLISECONDS)
                .readTimeout(10000L,TimeUnit.MILLISECONDS)
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }

    public static Context getContext(){
        return context;
    }

    //初始化GreenDao
    private static void initDatabase(){
//        DaoM
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(
                context,Constants.DATA_BASE_NAME);
        Database db = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();

    }

    //外部数据库
    private static void initDatabase1(){
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(
                context,"mobile.db");
        Database db = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        mobiledaoSession = daoMaster.newSession();
    }
    /**
     * mobileDaoSession
     */
    public static DaoSession getMobiledaoSession(){
        return  mobiledaoSession;
    }

    public static DaoSession getDaoSession(){
        return daoSession;
    }

    //增删改查  需要获取百宝箱Dao
    public void initDatabaseTable(){
//        MyUtil.getAllContacts();
        DaoSession daoSession = getDaoSession();
        BaiBaoXiangDao bbxDao = daoSession.getBaiBaoXiangDao();
        SharedPreferences sharedPreferences = MyUtil.getSharedPre();
        boolean b = sharedPreferences.getBoolean("inserttable",false);
        if (!b){
           String[] names = getResources().getStringArray(R.array.bbx_name);
            String[] icons = getResources().getStringArray(R.array.bbx_icon);
            for (int  i = 0; i < names.length ; i++){
                BaiBaoXiang bbx = new BaiBaoXiang(names[i],null,icons[i]);
                try {
                    long m = bbxDao.insert(bbx);
                    if (m > 0 ){
                         KLog.e("新增成功");
                    }else {
                        KLog.e("新增失败");
                    }
                }catch (Exception e ){
                    e.printStackTrace();
                }
            }
            //更新完了以后将共享首选项中的值改为true
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("inserttable",true);
            editor.commit();

        }else {

        }
    }
}
